<html>
 <head>
  <link href="./leetcode-problem.css" rel="stylesheet" type="text/css">
 </head>
 <body>
  <div class="question_difficulty">
   难度：Hard
  </div>
  <div>
   <h1 class="question_title">
    68. Text Justification
   </h1>
   <p>
    Given an array of words and a width&nbsp;
    <em>
     maxWidth
    </em>
    , format the text such that each line has exactly
    <em>
     maxWidth
    </em>
    characters and is fully (left and right) justified.
   </p>
   <p>
    You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces
    <code>
     ' '
    </code>
    when necessary so that each line has exactly
    <em>
     maxWidth
    </em>
    characters.
   </p>
   <p>
    Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
   </p>
   <p>
    For the last line of text, it should be left justified and no
    <strong>
     extra
    </strong>
    space is inserted between words.
   </p>
   <p>
    <strong>
     Note:
    </strong>
   </p>
   <ul>
    <li>
     A word is defined as a character sequence consisting&nbsp;of non-space characters only.
    </li>
    <li>
     Each word's length is&nbsp;guaranteed to be greater than 0 and not exceed
     <em>
      maxWidth
     </em>
     .
    </li>
    <li>
     The input array
     <code>
      words
     </code>
     &nbsp;contains at least one word.
    </li>
   </ul>
   <p>
    <strong>
     Example 1:
    </strong>
   </p>
   <pre>
<strong>Input:</strong>
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
<strong>Output:</strong>
[
&nbsp; &nbsp;"This &nbsp; &nbsp;is &nbsp; &nbsp;an",
&nbsp; &nbsp;"example &nbsp;of text",
&nbsp; &nbsp;"justification. &nbsp;"
]
</pre>
   <p>
    <strong>
     Example 2:
    </strong>
   </p>
   <pre>
<strong>Input:</strong>
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
<strong>Output:</strong>
[
&nbsp; "What &nbsp; must &nbsp; be",
&nbsp; "acknowledgment &nbsp;",
&nbsp; "shall be &nbsp; &nbsp; &nbsp; &nbsp;"
]
<strong>Explanation:</strong> Note that the last line is "shall be    " instead of "shall     be",
&nbsp;            because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.
</pre>
   <p>
    <strong>
     Example 3:
    </strong>
   </p>
   <pre>
<strong>Input:</strong>
words = ["Science","is","what","we","understand","well","enough","to","explain",
&nbsp;        "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
<strong>Output:</strong>
[
&nbsp; "Science &nbsp;is &nbsp;what we",
  "understand &nbsp; &nbsp; &nbsp;well",
&nbsp; "enough to explain to",
&nbsp; "a &nbsp;computer. &nbsp;Art is",
&nbsp; "everything &nbsp;else &nbsp;we",
&nbsp; "do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
]
</pre>
  </div>
  <div>
   <h1 class="question_title">
    68. 文本左右对齐
   </h1>
   <p>
    给定一个单词数组和一个长度&nbsp;
    <em>
     maxWidth
    </em>
    ，重新排版单词，使其成为每行恰好有&nbsp;
    <em>
     maxWidth
    </em>
    &nbsp;个字符，且左右两端对齐的文本。
   </p>
   <p>
    你应该使用&ldquo;贪心算法&rdquo;来放置给定的单词；也就是说，尽可能多地往每行中放置单词。必要时可用空格&nbsp;
    <code>
     ' '
    </code>
    &nbsp;填充，使得每行恰好有
    <em>
     maxWidth
    </em>
    &nbsp;个字符。
   </p>
   <p>
    要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配，则左侧放置的空格数要多于右侧的空格数。
   </p>
   <p>
    文本的最后一行应为左对齐，且单词之间不插入
    <strong>
     额外的
    </strong>
    空格。
   </p>
   <p>
    <strong>
     说明:
    </strong>
   </p>
   <ul>
    <li>
     单词是指由非空格字符组成的字符序列。
    </li>
    <li>
     每个单词的长度大于 0，小于等于&nbsp;
     <em>
      maxWidth
     </em>
     。
    </li>
    <li>
     输入单词数组
     <code>
      words
     </code>
     &nbsp;至少包含一个单词。
    </li>
   </ul>
   <p>
    <strong>
     示例:
    </strong>
   </p>
   <pre><strong>输入:</strong>
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
<strong>输出:</strong>
[
&nbsp; &nbsp;"This &nbsp; &nbsp;is &nbsp; &nbsp;an",
&nbsp; &nbsp;"example &nbsp;of text",
&nbsp; &nbsp;"justification. &nbsp;"
]
</pre>
   <p>
    <strong>
     示例&nbsp;2:
    </strong>
   </p>
   <pre><strong>输入:</strong>
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
<strong>输出:</strong>
[
&nbsp; "What &nbsp; must &nbsp; be",
&nbsp; "acknowledgment &nbsp;",
&nbsp; "shall be &nbsp; &nbsp; &nbsp; &nbsp;"
]
<strong>解释: </strong>注意最后一行的格式应为 "shall be    " 而不是 "shall     be",
&nbsp;    因为最后一行应为左对齐，而不是左右两端对齐。       
     第二行同样为左对齐，这是因为这行只包含一个单词。
</pre>
   <p>
    <strong>
     示例&nbsp;3:
    </strong>
   </p>
   <pre><strong>输入:</strong>
words = ["Science","is","what","we","understand","well","enough","to","explain",
&nbsp;        "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
<strong>输出:</strong>
[
&nbsp; "Science &nbsp;is &nbsp;what we",
  "understand &nbsp; &nbsp; &nbsp;well",
&nbsp; "enough to explain to",
&nbsp; "a &nbsp;computer. &nbsp;Art is",
&nbsp; "everything &nbsp;else &nbsp;we",
&nbsp; "do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
]
</pre>
  </div>
 </body>
</html>